home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / Misc / 3DShape.c next >
Encoding:
C/C++ Source or Header  |  1998-09-06  |  3.9 KB  |  137 lines

  1. /* Dice:  1> dcc -l0 -mD dpk.o tags.o 3DShape.c -o 3DShape
  2. **
  3. ** This demo uses the PenCircle() function to 'steal' circle coordinates
  4. ** and then uses them to create a 3D object.  This type of trick is legal
  5. ** but must be used carefully.
  6. */
  7.  
  8. #include <proto/dpkernel.h>
  9. #include <clib/colours_protos.h>
  10.  
  11. BYTE *ProgName      = "3D Shape Demo";
  12. BYTE *ProgAuthor    = "Paul Manias";
  13. BYTE *ProgDate      = "August 1998";
  14. BYTE *ProgCopyright = "DreamWorld Productions (c) 1998.  Freely distributable.";
  15. BYTE *ProgShort     = "Demonstration of pens being used to create dot-based shapes.";
  16.  
  17. struct GScreen  *Screen  = NULL;
  18. struct JoyData  *joydata = NULL;
  19.  
  20. void Demo(void);
  21.  
  22. WORD PixelNum = NULL;
  23.  
  24. #define AMT_PIXELS 10000
  25.  
  26. struct Entry3D {
  27.   WORD  XCoord;
  28.   WORD  YCoord;
  29.   LONG  Colour;
  30.   LONG  XCoord3D;
  31.   LONG  YCoord3D;
  32.   LONG  ZCoord3D;
  33. };
  34.  
  35. struct PixelList3D {
  36.   WORD   AmtEntries;
  37.   WORD   EntrySize;
  38.   struct Entry3D *Pixels;
  39. };
  40.  
  41. struct PixelList3D PixelList = {
  42.   NULL, sizeof(struct Entry3D), NULL
  43. };
  44.  
  45. /***************************************************************************/
  46.  
  47. void main(void) {
  48.  
  49.   if (Screen = Get(ID_SCREEN)) {
  50.      Screen->Attrib |= SCR_DBLBUFFER;
  51.    if (Init(Screen,NULL)) {
  52.     if (joydata = Get(ID_JOYDATA)) {
  53.      if (Init(joydata, NULL)) {
  54.       if (PixelList.Pixels = AllocMemBlock(sizeof(struct Entry3D) * AMT_PIXELS, MEM_DATA)) {
  55.          Display(Screen);
  56.          Demo();
  57.       }
  58.      }
  59.     }
  60.    }
  61.   }
  62.  
  63.   FreeMemBlock(PixelList.Pixels);
  64.   Free(Screen);
  65.   Free(joydata);
  66. }
  67.  
  68. /***************************************************************************/
  69.  
  70. LIBFUNC void StealPen(mreg(__a0) struct Bitmap *Bitmap, mreg(__d1) WORD X, mreg(__d2) WORD Y)
  71. {
  72.   if (PixelNum < AMT_PIXELS) {
  73.      PixelList.Pixels[PixelNum].XCoord = X;
  74.      PixelList.Pixels[PixelNum].YCoord = Y;
  75.      PixelList.Pixels[PixelNum].XCoord3D = X<<8;
  76.      PixelList.Pixels[PixelNum].YCoord3D = Y<<8;
  77.      PixelList.Pixels[PixelNum].ZCoord3D = 1;
  78.      PixelList.Pixels[PixelNum].Colour = FastRandom(Screen->Bitmap->AmtColours-1)+1;
  79.      PixelNum++;
  80.   }
  81. }
  82.  
  83. void Demo(void)
  84. {
  85.    WORD ZPos = 1<<8;
  86.    WORD i;
  87.  
  88.    /* This part alters the pen functions so that we can steal the
  89.    ** coordinates.  Once PenCircle() has been called, we set the
  90.    ** function pointers back to normal by calling SetPenShape()
  91.    ** and SetRGBPen().
  92.    */
  93.  
  94.    Screen->Bitmap->PenUCPixel = &StealPen;
  95.    Screen->Bitmap->DrawPen    = &StealPen;
  96.    PenCircle(Screen->Bitmap, 0, 0, 30, FALSE);
  97.    SetPenShape(Screen->Bitmap, PSP_PIXEL, 0);
  98.    SetRGBPen(Screen->Bitmap, 0xffffff);
  99.    PixelList.AmtEntries = PixelNum;
  100.  
  101.    do
  102.    {
  103.      Query(joydata);
  104.  
  105.      ZPos += joydata->YChange;
  106.      ZPos += joydata->XChange;
  107.      if (ZPos < 10)   ZPos = 10;   /* <-- This prevents division by 0 errors */
  108.      if (ZPos > 2000) ZPos = 2000;
  109.  
  110.      Clear(Screen->Bitmap);
  111.  
  112.      for (i=0; i < PixelNum; i++) {
  113.         PixelList.Pixels[i].XCoord = ((PixelList.Pixels[i].XCoord3D)/ZPos) + Screen->Width/2;
  114.         PixelList.Pixels[i].YCoord = ((PixelList.Pixels[i].YCoord3D)/ZPos) + Screen->Height/2;
  115.      }
  116.      DrawPixelList(Screen->Bitmap,(struct PixelList *)&PixelList);
  117.  
  118.      for (i=0; i < PixelNum; i++) {
  119.         PixelList.Pixels[i].XCoord = ((PixelList.Pixels[i].XCoord3D)/(ZPos/2)) + Screen->Width/2;
  120.         PixelList.Pixels[i].YCoord = ((PixelList.Pixels[i].YCoord3D)/(ZPos/2)) + Screen->Height/2;
  121.      }
  122.      DrawPixelList(Screen->Bitmap,(struct PixelList *)&PixelList);
  123.  
  124.      for (i=0; i < PixelNum-1; i++) {
  125.         PixelList.Pixels[i].XCoord = ((PixelList.Pixels[i].XCoord3D)/(ZPos*2)) + Screen->Width/2;
  126.         PixelList.Pixels[i].YCoord = ((PixelList.Pixels[i].YCoord3D)/(ZPos*2)) + Screen->Height/2;
  127.         PixelList.Pixels[i].Colour = PixelList.Pixels[i+1].Colour;
  128.      }
  129.      PixelList.Pixels[i].Colour = PixelList.Pixels[0].Colour;
  130.      DrawPixelList(Screen->Bitmap,(struct PixelList *)&PixelList);
  131.  
  132.      SwapBuffers(Screen);
  133.      WaitAVBL();
  134.    } while (!(joydata->Buttons & JD_LMB));
  135. }
  136.  
  137.